package com.dex4d.ui.main.socket;

import com.dex4d.utils.Log;
import com.google.gson.Gson;
import com.koushikdutta.async.callback.CompletedCallback;
import com.koushikdutta.async.callback.WritableCallback;
import com.koushikdutta.async.future.Future;
import com.koushikdutta.async.http.AsyncHttpClient;
import com.koushikdutta.async.http.WebSocket;

public class Client {

    private String host;
    private int port;
    private Future<WebSocket> future;
    private WebSocket webSocket;
    private WebSocket.StringCallback stringCallback;

    private RPCRequest rpcRequest;

    public Client(String host, int port) {
        this.host = host;
        this.port = port;
        setup();
    }

    public void sendRpcRequest(RPCRequest rpcRequest) {
        this.rpcRequest = rpcRequest;
        if (webSocket!=null&&webSocket.isOpen()) {
            String req = new Gson().toJson(rpcRequest);
            Log.i(req);
            webSocket.send(req);
        }
    }

    public void setStringCallback(WebSocket.StringCallback stringCallback) {
        this.stringCallback = stringCallback;
    }

    /**
     * 初始化设置
     */
    private void setup() {
        future = AsyncHttpClient
                .getDefaultInstance()
                .websocket(host + ":" + port, null,
                        new AsyncHttpClient.WebSocketConnectCallback() {
                            @Override
                            public void onCompleted(Exception ex, WebSocket socket) {
                                webSocket = socket;
                                handleConnectCompleted(ex, socket);
                            }
                        });
    }

    /**
     * 关闭
     */
    public void tearDown() {
        if (webSocket != null && webSocket.isOpen()) {
            webSocket.close();
        }
    }

    public void pause() {
        if (webSocket != null && webSocket.isOpen()) {
            webSocket.pause();
        }
    }

    public void resume() {
        if (webSocket != null && webSocket.isPaused()) {
            webSocket.resume();
        }
    }

    private void handleConnectCompleted(Exception ex, final WebSocket webSocket) {
        if (ex != null) {
            ex.printStackTrace();
            return;
        }

        if (rpcRequest!=null){
            webSocket.send(new Gson().toJson(rpcRequest)); //发送数据
        }

        //接收数据
        if (stringCallback != null) {
            webSocket.setStringCallback(stringCallback);
        }

        //关闭
        webSocket.setClosedCallback(new CompletedCallback() {
            @Override
            public void onCompleted(Exception ex) {
                Log.i("EndCallback");
            }
        });

        //结束
        webSocket.setEndCallback(new CompletedCallback() {
            @Override
            public void onCompleted(Exception ex) {
                Log.i("ClosedCallback");
            }
        });

        //写入
        webSocket.setWriteableCallback(new WritableCallback() {
            @Override
            public void onWriteable() {
                Log.i("WriteableCallback");
            }
        });

    }
}